package cn.db101.api.complex.service

import cn.db101.api.complex.component.Slf4j
import cn.db101.api.complex.component.Slf4j.Companion.log
import cn.hutool.core.img.ImgUtil
import cn.hutool.core.io.FileUtil
import cn.hutool.core.util.IdUtil
import cn.hutool.core.util.RandomUtil
import cn.hutool.core.util.StrUtil
import io.github.mymonstercat.ocr.InferenceEngine
import net.sourceforge.tess4j.Tesseract
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

/**
 * @author zooy
 * @since 2024/11/19-16:39
 */
@Service
@Slf4j
class OcrService {

    @Autowired
    private lateinit var tesseract: Tesseract

    @Autowired
    private lateinit var engine: InferenceEngine

    fun doOcr(mf: MultipartFile): String {

        // val ocrText = tesseract.doOCR(ImgUtil.toBufferedImage(ImgUtil.read(mf.inputStream)))
        val ocrText = engine.runOcr(FileUtil.writeFromStream(mf.inputStream, StrUtil.format("/whpe/tmp/{}.jpg", IdUtil.fastUUID())).path)
        log.info("ocrText: $ocrText")
        return ocrText.getStrRes().trim()
    }

}
